package com.sfx.handler.security;


import com.alibaba.fastjson.JSON;
import com.sfx.domain.ResponseResult;
import com.sfx.enums.AppHttpCodeEnum;
import com.sfx.utils.WebUtils;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description: 授权失败处理器
 * User: sfx
 * Date: 2023-07-12
 * Time: 14:59
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response,
                       AccessDeniedException accessDeniedException) throws IOException, ServletException {
        accessDeniedException.printStackTrace();
        ResponseResult responseResult = ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        //响应该前端
        WebUtils.renderString(response, JSON.toJSONString(responseResult));
    }
}